<template>
  <el-main>
    <el-form
      ref="form"
      :rules="rules"
      label-width="80px"
      :inline="true"
      size="small">
      <el-form-item>
        <el-input v-model="listParams.nickName" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="listParams.phone" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="searchButton">搜索</el-button>
        <el-button icon="el-icon-close" type="danger" plain @click="resetButton">重置</el-button>
        <el-button icon="el-icon-plus" type="success" @click="addBtn">新增用户</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table :data="tableData" :height="listParams.tableHeight" border stripe>
      <el-table-column prop="nickName" label="姓名"></el-table-column>
      <el-table-column prop="phone" label="手机号"></el-table-column>
      <el-table-column prop="email" label="邮箱"></el-table-column>
      <el-table-column prop="sex" label="性别" width="100px" align="center">
        <template slot-scope="scope">
          <el-button v-if="scope.row.sex == '0'" type="primary" plain round>男</el-button>
          <el-button v-else type="success" plain round>女</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200px" align="center">
        <template scope="scope">
          <el-button type="primary" size="small" icon="el-icon-edit" plain @click="editBtn(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" icon="el-icon-delete" plain @click="deleteBtn(scope.row)">删除
          </el-button>

        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChange"
      @current-change="currentChange"
      :current-page.sync="listParams.currentPage"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="listParams.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="listParams.total" background>
    </el-pagination>

    <!-- 新增或编辑弹框组件 -->
    <sys-dialog
      :title="dialog.title"
      :visible="dialog.visible"
      :width="dialog.width"
      :height="dialog.height"
      @onClose="onClose"
      @onConfirm="onConfirm"
    >
      <div slot="content">
        <!--    当做一个普通的form标签
                e1-form-item: 表单的每一项
                model: 表单绑定的数据
                ref : 相当于一个div的id ，唯一的
                rules :表单验证规则 -->
        <el-form :model="addModel" ref="addRef"
                 :rules="rules" label-width="80px" size="normal" style="margin-right: 40px;">

          <el-row>
            <el-col :span="12" :offset="0">
              <el-form-item prop="nickName" label="姓名">
                <el-input v-model="addModel.nickName"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" :offset="0">
              <el-form-item prop="phone" label="电话">
                <el-input v-model="addModel.phone"></el-input>
              </el-form-item>
            </el-col>
            </el-row>

          <el-row>
            <el-col :span="12" :offset="0">
              <el-form-item prop="email" label="邮箱">
                <el-input v-model="addModel.email"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" :offset="0">
              <el-form-item prop="username" label="账户">
                <el-input v-model="addModel.username"></el-input>
              </el-form-item>
            </el-col>
            </el-row>

          <el-row>
            <el-col v-if="addModel.type == '0' " :span="12" :offset="0">
              <el-form-item prop="password" label="密码">
                <el-input v-model="addModel.password"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" :offset="0">
              <el-form-item prop="sex" label="性别">
                <el-radio-group v-model="addModel.sex">
                  <el-radio :label="0">男</el-radio>
                  <el-radio :label="1">女</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12" :offset="0">
              <el-form-item prop="roleId" label="角色">
                <el-select style="width: 100%" v-model="addModel.roleId" placeholder="请选择角色">
                  <el-option
                    v-for="item in roleList"
                    :key="item.roleId"
                    :label="item.roleName"
                    :value="item.roleId">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>

    </sys-dialog>
  </el-main>
</template>

<script>
//这是弹窗组件
import SysDialog from '@/components/dialog/StstemDialog.vue'

import {getListApi, addUserApi, editUserApi, deleteUserApi, getRoleListApi,getRoleByUserIdApi} from '@/api/user'

export default {
  //注册弹框组件
  components: {
    SysDialog,
  },

  data() {
    return {
      //表单验证规则
      rules: {
        nickName: [{
          trigger: 'blur',
          required: true,
          message: '请填写姓名'
        }],
        phone: [{
          trigger: 'blur',
          required: true,
          message: '请填写手机号'
        }],
        email: [{
          trigger: 'blur',
          required: true,
          message: '请填写邮箱'
        }],
        sex: [{
          trigger: 'blur',
          required: true,
          message: '请选择性别'
        }],
        username: [{
          trigger: 'blur',
          required: true,
          message: '请填写账户'
        }],
        password: [{
          trigger: 'blur',
          required: true,
          message: '请填写密码'
        }],
        roleId: [{
          trigger: 'blur',
          required: true,
          message: '请选择角色'
        }]
      },
      //表单绑定属性
      addModel: {
        type: '', //区分新增或者编辑 0 新增 1 编辑
        userId: '',
        nickName: '',
        phone: '',
        email: '',
        sex: '',
        username: '',
        password: '',
        roleId:''
      },
      //弹框属性
      dialog: {
        title: '',
        visible: false,
        width: 800,
        height: 270,
      },

      listParams: {
        tableHeight: this.tableHeight = window.innerHeight - 220,
        total: 0,
        pageSize: 10,
        currentPage: 1,
        nickName: '',
        phone: ''
      },
      tableData: [],

      //角色列表
      roleList: [],
    }
  },
  created() {
    this.getList()
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      let res = await getRoleListApi();
      if (res && res.code == 200){
        this.roleList=res.data
      }
    }
    ,
//获取列表
    async getList() {
      let result = await getListApi(this.listParams)
      console.log('请求成功')
      console.log(result)
      if (result && result.code == 200) {
        this.tableData = result.data.records
        this.listParams.total = result.data.total
      }
    }
    ,
//弹框取消
    onClose() {
      this.dialog.visible = false;
    }
    ,

//弹框确定
    onConfirm() {
      this.$refs.addRef.validate(async (valid) => {
        if (valid) {
          let result = null
          if (this.addModel.type == '0') {
            result = await addUserApi(this.addModel)
          } else {
            result = await editUserApi(this.addModel)
          }
          if (result && result.code == 200) {
            //刷新表格
            this.getList()
            //信息提示
            this.$message({type: 'success', message: result.msg})
            this.dialog.visible = false;
          }
        }
      })
    }
    ,

//编辑用户
    async editBtn(row) {

      this.dialog.title = "编辑用户"
      this.dialog.visible = true
      //清空表单
      this.$resetForm('addRef', this.addModel)
      this.$objCopy(row, this.addModel)
      //查询用户的角色
      const res=await getRoleByUserIdApi({userId:row.userId})
      if (res && res.code == 200 && res.data){
        console.log(res.data)
        this.addModel.roleId=res.data.roleId
      }
      this.dialog.type = '1'

    }
    ,
//删除用户
    async deleteBtn(row) {
      let confirm = await this.$myconfirm("确定删除该用户吗？")
      if (confirm) {
        let result = await deleteUserApi({userId: row.userId})
        if (result && result.code == 200) {
          //刷新表格
          this.getList()
          //信息提示
          this.$message({type: 'success', message: result.msg})
          this.dialog.visible = false;
        }
      }
    }
    ,
    currentChange(val) {
      this.listParams.currentPage = val;
      this.getList()
    }
    ,
    sizeChange(val) {
      this.listParams.pageSize = val;
      this.getList();
    }
    ,
//搜索按钮
    searchButton() {
      this.getList();
    }
    ,
    resetButton() {
      this.listParams.nickName = ''
      this.listParams.phone = ''
      this.getList();
    }
    ,
//新增用户按钮
    addBtn() {
      this.dialog.title = "新增用户"
      this.dialog.visible = true
      //清空表单
      this.$resetForm('addRef', this.addModel)
      this.addModel.type = '0'

    }
    ,

  },
  mounted() {
    this.$nextTick(() => {
      this.tableHeight = window.innerHeight - 220
    })
  }
}
</script>

<style lang="scss" scoped></style>
